package cn.itcast.service.impl;

import cn.itcast.dao.AddressDao;
import cn.itcast.pojo.Address;
import cn.itcast.service.AddressService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressDao addressDao;


    /**
     * 查询地址列表
     *
     * @return
     */

    @Override
    public List<Address> queryPageaddress() {
        return addressDao.queryPageAddress();
    }

    /**
     * 新增分公司
     *
     * @param address
     */
    @Transactional
    @Override
    public void addAddress(Address address) {
        addressDao.addAddress(address);
    }

    /**
     * 弹出修改框数据回显
     *
     * @param id
     * @return
     */
    @Override
    public Address queryById(Integer id) {
        return addressDao.queryById(id);

    }

    /**
     * 修改地址
     *
     * @param address
     */
    @Transactional
    @Override
    public void updateAddress(Address address) {
        addressDao.updateAddress(address);
    }

    /**
     * 删除地址
     *
     * @param id
     */
    @Transactional
    @Override
    public void deleteAddress(Integer id) {
        addressDao.deleteAddress(id);
    }

    /**
     * 是否启用地址
     *
     * @param id
     */
    @Override
    public void dom_Storage(Integer id) {
        Address address = addressDao.queryById(id);
        Integer enable = address.getEnable();
        Integer old = 0;
        if (enable == 1) {
            old = 0;
            addressDao.dom_Storage(old, id);
            return;
        }
        old = 1;
        addressDao.dom_Storage(old, id);

    }


    public List<Address> findAll() {
        return addressDao.findAll();
    }

    /* 查询所有分院地址
     * @return
     */
    @Override
    public List<Address> findAddress() {
        //查询分院地址
        List<Address> addressList = this.addressDao.findAll();

        //判断分院地址是否可用
        List<Address> newAddressList = new ArrayList<>();
        for (Address address : addressList) {
            if (address.getEnable() == 1) {
                //该地址可用
                newAddressList.add(address);
            }
        }
        return newAddressList;
    }
}
